package mqtt

import (
	"encoding/json"
	"fmt"
	"github.com/astaxie/beego/logs"
	gomqtt "github.com/eclipse/paho.mqtt.golang"
)

var C = NewClient("no1")

//func PublishTerminalCmdRequest() {
//	if err := C.Connect(); err != nil {
//		logs.Error(err)
//		return
//	}
//
//	data := TerminalCmdRequest {
//		MessageId: "cc46e9b9ebca48b9a2ec6f24ecb0875f",
//		MessageName: "terminalCmdRequest",
//		ThingId: "d2cf004f6824456b9ee8009d53400135",
//		MachineSn: "EQ001",
//		SiteId: 3,
//		ReportTime: 11233147980,
//		MessageType: "Andon",
//		MessageText: "安灯(IT三工段整机包装X08Y08(李凌00199037/13823553503)呼叫您",
//	}
//	jsonData, err := json.Marshal(data)
//	if err != nil {
//		logs.Error("failed to marshal huawei message")
//	}
//	go func() {
//		for {
//			logs.Info("pub start")
//			if err := C.Publish("5GLoc/HUAWEI/6623/员工/tag123/TerminalCmdRequest", 1, true, jsonData); err != nil {
//				logs.Error(err)
//				return
//			}
//			time.Sleep(time.Second * 10)
//		}
//	}()
//}

func Listen() {
	if err := C.Connect(); err != nil {
		logs.Error(err)
		return
	}
	//topic0 := "5GLoc/LocalSense/+/+/+/AttributeDataReport"
	//topic1 := "5GLoc/LocalSense/+/+/+/TagAreaChangedReport"
	//topic2 := "5GLoc/LocalSense/+/+/+/AlarmReport"
	topic3 := "5GLoc/HUAWEI/+/+/+/TerminalCmdRequest"
	//topic4 := "5GLoc/HUAWEI/+/+/+/TerminalCmdReply"

	var msgRcvd gomqtt.MessageHandler = func(client gomqtt.Client, message gomqtt.Message) {
		fmt.Printf("Received message on topic: %s Message: %s\n", message.Topic(), message.Payload())
		unmarshalMessage(message.Topic(), message.Payload())
	}

	if err := C.SubscribeOne(1, msgRcvd, topic3); err != nil {
		logs.Error(err)
	}
	logs.Info("topic subscribed")
}

func unmarshalMessage(topic string, payload []byte) {
	logs.Info("Topic is ", checkSuffix(topic))
	switch checkSuffix(topic) {
	case "AttributeDataReport":
		msg := &AttributeDataReport{}
		if err := json.Unmarshal(payload, msg); err != nil {
			logs.Error("failed to unmarshal AttributeDataReport")
		}
		logs.Info("Message name: ", msg.MessageName)
	case "TagAreaChangedReport":
		msg := &TagAreaChangedReport{}
		if err := json.Unmarshal(payload, msg); err != nil {
			logs.Error("failed to unmarshal TagAreaChangedReport")
		}
		logs.Info("Message name: ", msg.MessageName)
	case "AlarmReport":
		msg := &AlarmReport{}
		if err := json.Unmarshal(payload, msg); err != nil {
			logs.Error("failed to unmarshal AlarmReport")
		}
		logs.Info("Message name: ", msg.MessageName)
	case "TerminalCmdReply":
		msg := &TerminalCmdReply{}
		if err := json.Unmarshal(payload, msg); err != nil {
			logs.Error("failed to unmarshal TerminalCmdReply")
		}
		logs.Info("Message name: ", msg.MessageName)
	case "TerminalCmdRequest":
		msg := &TerminalCmdRequest{}
		if err := json.Unmarshal(payload, msg); err != nil {
			logs.Error("failed to unmarshal TerminalCmdRequest")
		}
		logs.Info("Message name: ", msg.MessageName)
	}
}

func checkSuffix(topic string) string {
	var str = []byte(topic)
	n := len(topic)
	for i := n - 1; i >= 0; i-- {
		if str[i] == '/' {
			str = str[i+1:]
			break
		}
	}
	return string(str)
}
